package utils.file;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Slf4j
public class FileUtil {

	/**
	 * 下载附件
	 *
	 * @param fileName
	 * @param ins
	 * @param response
	 * @throws IOException
	 */
	public static void download(String fileName, InputStream ins, HttpServletResponse response) throws IOException {
		ServletOutputStream out = null;
		response.setContentType("application/octet-stream;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ new String(fileName.getBytes(), "UTF-8") + "\"");
			out = response.getOutputStream();
			int len = 0;
			byte buf[] = new byte[1024];
			while ((len = ins.read(buf, 0, 1024)) != -1) {
				out.write(buf, 0, len);
			}
			out.flush();
		} catch (IOException e) {
			log.error("", e);
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 下载附件
	 *
	 * @param fileName
	 * @param datas
	 * @param response
	 * @throws IOException
	 */
	public static void download(String fileName, byte[] datas, HttpServletResponse response) throws IOException {
		ServletOutputStream out = null;
		response.setContentType("application/octet-stream;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ new String(fileName.getBytes(), "UTF-8") + "\"");
			out = response.getOutputStream();
			out.write(datas);
			out.flush();
		} catch (IOException e) {
			log.error("", e);
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}


	public static void downloadFile(HttpServletResponse response, String downloadName, String filePath) throws IOException {
		ServletOutputStream out = null;
		InputStream input = null;
		response.setContentType("application/octet-stream;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ new String(downloadName.getBytes(), "UTF-8") + "\"");
			input = FileUtil.class.getResourceAsStream(filePath);
			OutputStream output = response.getOutputStream();
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = input.read(buff)) > -1) {
				output.write(buff, 0, len);
			}
			output.flush();
		} catch (IOException e) {
			log.error("", e);
			throw e;
		} finally {
			if (out != null) {
				out.close();
			}
			if (input != null) {
				input.close();
			}
		}
	}

	/**
	 * 文件转化为字节数组
	 *
	 * @param pathAndFileName
	 * @return
	 */
	public static byte[] file2bytes(String pathAndFileName) {
		byte[] bytes = null;
		FileInputStream stream = null;
		ByteArrayOutputStream out = null;
		File file = new File(pathAndFileName);
		if (!file.exists()) {
			return bytes;
		}
		try {
			stream = new FileInputStream(file);
			out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = stream.read(b)) != -1) {
				out.write(b, 0, n);
			}
			bytes = out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stream != null) {
					stream.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bytes;
	}

	/**
	 * 文件转化为字节数组
	 */
	public static byte[] file2bytes(File file) {
		byte[] bytes = null;
		FileInputStream stream = null;
		ByteArrayOutputStream out = null;
		if (!file.exists()) {
			return bytes;
		}
		try {
			stream = new FileInputStream(file);
			out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = stream.read(b)) != -1) {
				out.write(b, 0, n);
			}
			bytes = out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stream != null) {
					stream.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bytes;
	}


}
